package team.fjut.cf.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import team.fjut.cf.mapper.AcbRecordMapper;
import team.fjut.cf.mapper.UserBaseInfoMapper;
import team.fjut.cf.mapper.UserCheckInMapper;
import team.fjut.cf.pojo.enums.ACBChangeType;
import team.fjut.cf.pojo.po.AcbRecord;
import team.fjut.cf.pojo.po.UserBaseInfo;
import team.fjut.cf.pojo.po.UserCheckIn;
import team.fjut.cf.service.UserCheckInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.fjut.cf.utils.TimeUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author axiang [2019/10/30]
 */
@Service
public class UserCheckInServiceImpl implements UserCheckInService {
    @Resource
    private UserCheckInMapper userCheckInMapper;

    @Resource
    private UserBaseInfoMapper userBaseInfoMapper;

    @Resource
    private AcbRecordMapper acbRecordMapper;

    @Override
    public Integer insert(UserCheckIn userCheckIn) {
        return userCheckInMapper.insertSelective(userCheckIn);
    }

    @Override
    public List<UserCheckIn> select(String username) {
        Example example = new Example(UserCheckIn.class);
        example.orderBy("checkTime").desc();
        example.createCriteria().andEqualTo("username", username);
        return userCheckInMapper.selectByExample(example);
    }

    @Override
    public boolean isTodayUserCheckIn(String username) {
        Example example = new Example(UserCheckIn.class);
        example.createCriteria().andEqualTo("username", username)
                .andGreaterThanOrEqualTo("checkTime", TimeUtils.initDateByDay())
                .andLessThanOrEqualTo("checkTime", new Date());
        return userCheckInMapper.selectCountByExample(example) >= 1;
    }

    @Override
    public List<UserCheckIn> pagesByUsername(String username, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserCheckIn> userCheckIns = userCheckInMapper.selectByUsername(username);
        return userCheckIns;
    }

    @Override
    public Integer countByUsername(String username) {
        Example example = new Example(UserCheckIn.class);
        example.createCriteria().andEqualTo("username", username);
        return userCheckInMapper.selectCountByExample(example);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public Boolean checkInAndAwardACB(String username, String IpAddress, Integer awardACB) {
        // t_user_checkin
        UserCheckIn userCheckIn = new UserCheckIn();
        userCheckIn.setUsername(username);
        userCheckIn.setIpAddress(IpAddress);
        userCheckIn.setCheckTime(new Date());
        userCheckIn.setInfo(ACBChangeType.CHECK_IN.getName());
        int ans1 = userCheckInMapper.insertSelective(userCheckIn);
        // t_acb_record
        Integer acb = userBaseInfoMapper.selectByUsername(username).getAcb();
        AcbRecord acbRecord = new AcbRecord();
        acbRecord.setUsername(username);
        acbRecord.setAcbChange(awardACB);
        acbRecord.setCurrentAcb(acb);
        acbRecord.setReason(ACBChangeType.CHECK_IN.getId());
        acbRecord.setTime(new Date());
        acbRecord.setMark(ACBChangeType.CHECK_IN.getName());
        int ans2 = acbRecordMapper.insertSelective(acbRecord);
        // t_user_base_info
        int ans3 = userBaseInfoMapper.updateACB(username, awardACB);
        return ans1 == 1 && ans2 == 1 && ans3 == 1;
    }


}
